package com.ruoyi.lipin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.lipin.domain.LpProduct;
import com.ruoyi.lipin.domain.bo.LpProductBo;
import com.ruoyi.lipin.domain.vo.LpProductVo;
import com.ruoyi.lipin.mapper.LpProductMapper;
import com.ruoyi.lipin.service.ILpProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@RequiredArgsConstructor
@Service
public class LpProductServiceImpl implements ILpProductService {

    private final LpProductMapper baseMapper;

    /**
     * 查询商品信息
     */
    @Override
    public LpProductVo queryById(Long productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 查询商品信息列表
     */
    @Override
    public TableDataInfo<LpProductVo> queryPageList(LpProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LpProduct> lqw = buildQueryWrapper(bo);
        Page<LpProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品信息列表
     */
    @Override
    public List<LpProductVo> queryList(LpProductBo bo) {
        LambdaQueryWrapper<LpProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LpProduct> buildQueryWrapper(LpProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LpProduct> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), LpProduct::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, LpProduct::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), LpProduct::getImageUrl, bo.getImageUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceUrl()), LpProduct::getSourceUrl, bo.getSourceUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getSourcePlatform()), LpProduct::getSourcePlatform, bo.getSourcePlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), LpProduct::getDescription, bo.getDescription());
        lqw.eq(bo.getBrowseCount() != null, LpProduct::getBrowseCount, bo.getBrowseCount());
        lqw.eq(bo.getClickCount() != null, LpProduct::getClickCount, bo.getClickCount());
        return lqw;
    }

    /**
     * 新增商品信息
     */
    @Override
    public Boolean insertByBo(LpProductBo bo) {
        LpProduct add = BeanUtil.toBean(bo, LpProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductId(add.getProductId());
        }
        return flag;
    }

    /**
     * 修改商品信息
     */
    @Override
    public Boolean updateByBo(LpProductBo bo) {
        LpProduct update = BeanUtil.toBean(bo, LpProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LpProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<LpProduct> filterProducts(Map<String, Object> filterParams, PageQuery pageQuery) {
        QueryWrapper<LpProduct> wrapper = new QueryWrapper<>();
        // 这里根据 filterParams 构建查询条件
        if (filterParams.containsKey("name")) {
            wrapper.like("name", filterParams.get("name"));
        }
        if (filterParams.containsKey("sourcePlatform")) {
            wrapper.eq("source_platform", filterParams.get("sourcePlatform"));
        }
        if (filterParams.containsKey("minPrice") && filterParams.containsKey("maxPrice")) {
            wrapper.between("price", filterParams.get("minPrice"), filterParams.get("maxPrice"));
        }
        Page<LpProduct> page = baseMapper.selectPage(pageQuery.build(), wrapper);
        return TableDataInfo.build(page);
    }

    @Override
    public List<LpProduct> getHotProducts(int count) {
        QueryWrapper<LpProduct> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("browse_count", "click_count");
        wrapper.last("LIMIT " + count);
        return baseMapper.selectList(wrapper);
    }
}
